﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using OrderLibrary;

namespace OrderWinForm
{
    
    public partial class Form1 : Form
    {     
        public OrderService Orders = new OrderService();
        public Order O1;
        public Order O2;
        public Order O3;
        public OrderDetails OD1;
        public OrderDetails OD2;
        public OrderDetails OD3;

        public Form1()
        {
            InitializeComponent();
         O1 = new Order(1, "cheese", "Jerry");
         O2 = new Order(2, "milk", "Tom");
         O3 = new Order(3, "meat", "Spike");
         OD1 = new OrderDetails(1, "cheese", 2, 3);
         OD2 = new OrderDetails(1, "milk", 3, 4);
         OD3 = new OrderDetails(1, "meat", 4, 5);
            O1.Orderss.Add(OD1);
            O2.Orderss.Add(OD2);
            O3.Orderss.Add(OD3);
            Orders.orderData.Add(O1);
            O1.theWholePrice();
            Orders.orderData.Add(O2);
            O2.theWholePrice();
            Orders.orderData.Add(O3);
            O3.theWholePrice();
            OrderBindingSource.DataSource = Orders.orderData;
            this.flowLayoutPanel1.Resize += new System.EventHandler(this.flowLayoutPanel1_Resize);

    }



        private void button2_Click(object sender, EventArgs e)//删除订单
        {
            int Ono1 = (int)dataGridView1.CurrentRow.Cells[0].Value;
            for (int i = Orders.orderData.Count - 1; i >= 0; i--)
            {
                if (Orders.orderData[i].Ono == Ono1)
                {
                    Orders.orderData.Remove(Orders.orderData[i]);
                }
            }
            OrderBindingSource.ResetBindings(false);
        }

        private void button3_Click(object sender, EventArgs e)//查询订单
        {

            Form3 SearchOrder = new Form3();
            SearchOrder.Show(this);
        }

        private void button5_Click(object sender, EventArgs e)//修改订单
        {
            Order UO1 = new Order();
            OrderDetails UOD1 = new OrderDetails();
            int Ono2 = (int)dataGridView1.CurrentRow.Cells[0].Value;
            for(int j = Orders.orderData.Count - 1; j >= 0; j--)
            {
                if (Orders.orderData[j].Ono == Ono2)
                {
                    UO1 = Orders.orderData[j];
                }
            }
            int Oid1 = (int)dataGridView2.CurrentRow.Cells[0].Value;
            for(int k =  UO1.Orderss.Count - 1; k >= 0; k--)
            {
                if (UO1.Orderss[k].OId == Oid1)
                {
                    UOD1 = UO1.Orderss[k];
                }
            }
            Form2 ChangeOrder = new Form2(UO1,UOD1);
            ChangeOrder.Show(this);
        }

        private void button4_Click(object sender, EventArgs e)//导入订单
        {
            List<Order> OD = new List<Order>();
            XmlSerializer toDexml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("OrderXml.xml", FileMode.Open)) 
            {
                OD = (List<Order>)toDexml.Deserialize(fs);
                OD.ForEach(i =>
                {
                    if (!Orders.orderData.Contains(i))
                    {
                        Orders.orderData.Add(i);
                    }
                });
            }
        }

        private void button6_Click(object sender, EventArgs e)//导出订单
        {
            XmlSerializer toXml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("OrderXml.xml", FileMode.Create))
            {
                toXml.Serialize(fs, Orders.orderData);
            }
        }
        int oldwidth = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            oldwidth = this.Width;
        }
        private void flowLayoutPanel1_Resize(object sender,EventArgs e)
        {
            if (oldwidth != this.flowLayoutPanel1.Width)
            {
                AddButton.Size = new Size(this.flowLayoutPanel1.Width - 25, this.AddButton.Height);
                oldwidth = this.Width;
            }
        }
        private void AddButton_Click(object sender, EventArgs e)
        {
            Form2 AddOrder = new Form2();
            AddOrder.Show(this);
        }

        private void exButton_Click(object sender, EventArgs e)
        {
            XmlSerializer toXml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("OrderXml.xml", FileMode.Create))
            {
                toXml.Serialize(fs, Orders.orderData);
            }
        }
    }
}
